package com.schedule;

import com.Mapper.LandRepeatMapper.LandRepeatMapper;
import com.common.HttpRequest;
import com.common.publicMethod;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.param.LandParam.LandBaseParam;
import com.param.LandParam.LandDuplicationParam;
import com.util.RestTemplateUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Component
public class LandRepeatSchedule {
    private static final String USER_LIST_KEY = "user:list";

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private publicMethod publicMethod;
    @Autowired
    private LandRepeatMapper landRepeatMapper;
    // 0 0 0-23 * * ?


    //将土地的竖表和主表 ,取出想要的字段 , 组成一张新的横表.
    @XxlJob("combineLandOne")
    public void combineLandOne() throws JsonProcessingException {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = null;
        try {
            //参数
            String str = "{\"phone\":\"18915047090\",\"password\":\"111111\"}";
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/login";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
        }
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        List<String> cookieList = httpHeaders.get("Set-Cookie");
        String sqlCookie = cookieList.get(0);
        String repeatNum = publicMethod.getNowDateShort();
        log.error("one开始:{}", System.currentTimeMillis());
        long time = 0;
        Long replaceGoalNum = Long.valueOf("1" + repeatNum.replace("-", "") + "00000");
        //创建最终需要的集合
        //获取城市列表
        List<String> cityList = publicMethod.getAllCity(sqlCookie);
        cityList.add("测试");
        //ArrayList<String> cityList = new ArrayList<>();
       // cityList.add("巴彦淖尔市");


        List<Integer> columnListOne = new ArrayList<>();
        columnListOne.add(3);
        columnListOne.add(98);
        columnListOne.add(276);
        columnListOne.add(24);

        //TODO  城市集合以后走查询获取 .可配置
        for (String citys : cityList) {
            List<LandDuplicationParam> needList = new ArrayList<>();
            List<LandBaseParam> allBaseData = landRepeatMapper.getLandByAllCountry(citys, columnListOne);

            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = allBaseData.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));
            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());
                allLandParam.setLandCaption(param.getLandCaption());
                allLandParam.setCityCaption(param.getCityCaption());
                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //宗地编号
                    if (StringUtils.equals("98", columnValue.getColumnId())) {
                        String values=columnValue.getValue();
                        String modifiedString = values.replaceAll("[^\\p{IsAlphabetic}\\p{IsDigit}]", "");
                        allLandParam.setLandNum(modifiedString);
                      //  allLandParam.setLandNum(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }
                    //土地交易现状
                    if (StringUtils.equals("24", columnValue.getColumnId())) {
                        if (StringUtils.equals(columnValue.getValue(), "已成交") ||
                                StringUtils.equals(columnValue.getValue(), "未上市") ||
                                StringUtils.equals(columnValue.getValue(), "正在交易")
                        ) {
                            allLandParam.setLandTradeState(columnValue.getValue());
                        }
                    }
                }
                allList.add(allLandParam);
            }
            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> map = allList.stream().filter(
                    param -> !StringUtils.isEmpty(param.getLandNum()) && StringUtils.equals(param.getLandTradeState(), "已成交") || StringUtils.equals(param.getLandTradeState(), "未上市") || StringUtils.equals(param.getLandTradeState(), "正在交易"))
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getRegion(), s.getLandNum(), s.getSource())
                    ));

            // 找出那些有重复数据的列表
            List<List<LandDuplicationParam>> duplicates = map.values().stream()
                    .filter(list -> list.size() > 1)
                    .collect(Collectors.toList());


            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {

                    LandDuplicationParam param = new LandDuplicationParam();
                    param.setCount(duplicate.size());
                    param.setCityCaption(duplicate.get(0).getCityCaption());
                    param.setLandNum(duplicate.get(0).getLandNum());
                    String landCaption = "";
                    String ids = "";
                    List<Long> idList = new ArrayList<>();
                    for (int i = 0; i < duplicate.size(); i++) {
                        if (StringUtils.isEmpty(duplicate.get(i).getLandNum()) || StringUtils.isEmpty(duplicate.get(i).getRegion())) {
                            continue;
                        }
                        landCaption = landCaption + String.valueOf(duplicate.get(i).getLandCaption()) + ";";

                        idList.add(duplicate.get(i).getId());
                        ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                    }
                    if (!CollectionUtils.isEmpty(idList)) {
                        param.setIdList(ids);
                        param.setLandCaption(landCaption);
                        param.setCheckDate(publicMethod.getNowDate());
                        param.setDuplicationReason("1");
                        param.setRepeatGoal(replaceGoalNum++);
                        needList.add(param);
                    }
                }
            }

          //  if (!CollectionUtils.isEmpty(needList)) {
                insertCombineLand(needList, sqlCookie, "1", citys);
       //     }
       /*     List<LandDuplicationParam> needList = new ArrayList<>();

            List<Integer> columnListOne = new ArrayList<>();
            columnListOne.add(3);
            columnListOne.add(98);
            columnListOne.add(276);
            columnListOne.add(24);
            //     columnListOne.add(77);


            List<LandBaseParam> landList = new ArrayList<>();


            JSONObject param1 = new JSONObject();
            JSONObject param3 = new JSONObject();
            JSONObject param4 = new JSONObject();
            JSONObject param5 = new JSONObject();
            JSONObject param6 = new JSONObject();
            param6.put("State", 1);
            param6.put("ColumnId", "77");
            param6.put("Value>=", "2023-11-01");
            param6.put("@column", "Buildingid");
            param5.put("Land_Shu", param6);
            param5.put("from", "Land_Shu");
            param4.put("@column", "count(1):num");
            param3.put("State", 1);
            param3.put("CityCaption", citys);
            param3.put("id@", "/Land_Shu/Buildingid");
            param4.put("State", 1);
            param4.put("Buildingid{}@", "sql");

            param4.put("Columnid{}", columnListOne);
            param1.put("join", "&/Internal_Land_BaseInfo_Main/id@");
            param1.put("Land_Shu", param4); //左表
            param1.put("Internal_Land_BaseInfo_Main", param3); //右表

            net.sf.json.JSONObject param2 = new net.sf.json.JSONObject();

            param2.put("sql@", param5);
            param2.put("[]", param1);

            String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
            net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
            int newCount = 0;
            if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
                for (Object s1 : sqlData.getJSONArray("[]")) {
                    net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Shu");
                    newCount = details.getInt("num");
                }
            }
            //CityCaption,LandCaption
            param3.put("@column", "id,LandCaption");
            param3.put("@order", "id-");
            param4.put("@column", "Columnid,Value,Buildingid");
            param4.put("Buildingid{}@", "sql");
            param1.put("Land_Shu", param4); //左表
            param1.put("Internal_Land_BaseInfo_Main", param3); //右表
            int cycleNum = 0;
            if (newCount > 50000) {
                cycleNum = newCount / 50000 + 1;
            }
            for (int page = 0; page <= cycleNum; page++) {

                param1.put("page", page);
                param1.put("count", 50000);
                param2.put("[]", param1);

                long start = System.currentTimeMillis();
                String ssr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
                long end = System.currentTimeMillis();
                long differ = end - start;
                time = time + differ;


                ObjectMapper mapper = new ObjectMapper();
                JsonNode rootNode = mapper.readTree(ssr);
                JsonNode jsonNode = rootNode.get("[]");
                if (jsonNode != null) {

                    for (JsonNode node : jsonNode) {

                        JsonNode detailsMain = node.get("Internal_Land_BaseInfo_Main");
                        JsonNode detailsShu = node.get("Land_Shu");

                        LandBaseParam duplication = new LandBaseParam();
                        if (detailsMain.has("id")) {
                            JsonNode longValueNode = detailsMain.get("id");
                            if (longValueNode != null) {
                                // 安全地获取long值
                                long id = longValueNode.asLong();
                                duplication.setId(id);
                            }
                            JsonNode cityValue = detailsMain.get("CityCaption");
                            if (cityValue != null) {
                                // 安全地获取String的值
                                String city = cityValue.asText();
                                duplication.setCityCaption(city);
                            }

                            JsonNode Columnid = detailsShu.get("Columnid");
                            if (Columnid != null) {
                                // 安全地获取String的值
                                String columnid = Columnid.asText();
                                duplication.setColumnId(columnid);
                            }

                            JsonNode Value = detailsShu.get("Value");
                            if (Value != null) {
                                // 安全地获取String的值
                                String value = Value.asText();
                                duplication.setValue(value);
                            }
                            landList.add(duplication);
                        }
                    }
                }
            }

            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = landList.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));

            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());
                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //宗地编号
                    if (StringUtils.equals("98", columnValue.getColumnId())) {
                        allLandParam.setLandNum(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }
                    //土地交易现状
                    if (StringUtils.equals("24", columnValue.getColumnId())) {
                        if (StringUtils.equals(columnValue.getValue(), "已成交") ||
                                StringUtils.equals(columnValue.getValue(), "未上市") ||
                                StringUtils.equals(columnValue.getValue(), "正在交易")
                        ) {
                            allLandParam.setLandTradeState(columnValue.getValue());
                        }
                    }
                }
                allList.add(allLandParam);
            }

            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> map = allList.stream().filter(
                    param -> !StringUtils.isEmpty(param.getLandNum()) && StringUtils.equals(param.getLandTradeState(), "已成交") || StringUtils.equals(param.getLandTradeState(), "未上市") || StringUtils.equals(param.getLandTradeState(), "正在交易"))
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getRegion(), s.getLandNum(), s.getSource())
                    ));


            // 找出那些有重复数据的列表
            List<List<LandDuplicationParam>> duplicates = map.values().stream()
                    .filter(list -> list.size() > 1)
                    .collect(Collectors.toList());


            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {
                  //判断土地交易现状  三个状态至少出现两次
                    Set<String> unique = duplicate.stream()
                            .map(LandDuplicationParam::getLandTradeState)
                            .collect(Collectors.toSet());
                    if (unique.size() > 1) {

                        LandDuplicationParam param = new LandDuplicationParam();
                        param.setCount(duplicate.size());
                        param.setCityCaption(citys);
                        param.setLandNum(duplicate.get(0).getLandNum());
                        //   String landCaption = "";
                        String ids = "";
                        List<Long> idList = new ArrayList<>();
                        for (int i = 0; i < duplicate.size(); i++) {
                            if (StringUtils.isEmpty(duplicate.get(i).getLandNum()) || StringUtils.isEmpty(duplicate.get(i).getRegion())) {
                                continue;
                            }

                            idList.add(duplicate.get(i).getId());
                            ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                        }
                        if (!CollectionUtils.isEmpty(idList)) {
                            //根据idList  获取landCaption
                            String landCaption = this.getLandCaptionById(idList, sqlCookie);
                            param.setLandCaption(landCaption);
                            param.setIdList(ids);
                            param.setCheckDate(publicMethod.getNowDate());
                            param.setRepeatGoal(replaceGoalNum++);
                            param.setDuplicationReason("1");
                            needList.add(param);
                        }
                    }
                }
                insertCombineLand(needList, sqlCookie, "1", citys);
            }*/

        }

        log.error("one:{}", System.currentTimeMillis());
        log.error("timeOne:{}", time);
    }

    //将土地的竖表和主表 ,取出想要的字段 , 组成一张新的横表.
    @XxlJob("combineLandTwo")
    public void combineLandTwo() throws JsonProcessingException {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = null;
        try {
            //参数
            String str = "{\"phone\":\"18915047090\",\"password\":\"111111\"}";
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/login";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
        }
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        List<String> cookieList = httpHeaders.get("Set-Cookie");
        String sqlCookie = cookieList.get(0);
        log.error("Two开始:{}", System.currentTimeMillis());
        long time = 0;
        String repeatNum = publicMethod.getNowDateShort();
        Long replaceGoalNum = Long.valueOf("2" + repeatNum.replace("-", "") + "00000");
        //创建最终需要的集合
        //获取城市列表
        List<String> cityList = publicMethod.getAllCity(sqlCookie);
      //  List<String> cityList = new ArrayList<>();
        List<Integer> columnListTwo = new ArrayList<>();
        columnListTwo.add(3);
        columnListTwo.add(276);
        columnListTwo.add(122);
        columnListTwo.add(12);
        cityList.add("测试");
        for (String citys : cityList) {
            List<LandDuplicationParam> needList = new ArrayList<>();
            List<LandBaseParam> allBaseData = landRepeatMapper.getLandByAllCountry(citys, columnListTwo);

            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = allBaseData.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));

            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());

                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }
                    //底价
                    if (StringUtils.equals("122", columnValue.getColumnId())) {
                        //todo  需要校验中文情况 ,抛出异常 ,然后禁止处理这条数据
                        if (!StringUtils.isEmpty(columnValue.getValue())) {
                            if (StringUtils.contains(columnValue.getValue(), ".")) {
                                String strPrice = columnValue.getValue();
                                int index = columnValue.getValue().indexOf(".");
                                if (index != -1) {
                                    strPrice = strPrice.substring(0, index);
                                }
                                // 将处理后的字符串转换为BigInteger
                                //  BigInteger number = new BigInteger(strPrice);
                                allLandParam.setPrice(Long.valueOf(strPrice));

                            } else {
                                allLandParam.setPrice(Long.valueOf(columnValue.getValue()));
                            }
                        }
                    }
                    //占地面积
                    if (StringUtils.equals("12", columnValue.getColumnId())) {
                        if (!StringUtils.isEmpty(columnValue.getValue())) {
                            if (StringUtils.contains(columnValue.getValue(), ".")) {
                                double number = Double.parseDouble(columnValue.getValue());
                                int truncatedNumber = (int) number;
                                allLandParam.setLandArea(truncatedNumber);
                            } else {
                                allLandParam.setLandArea(Integer.valueOf(columnValue.getValue()));
                            }
                        }
                    }

                }
                allList.add(allLandParam);
            }

            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> group = allList.stream()
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getLandArea(), s.getPrice(), s.getRegion())
                    ));

            // 过滤出含有超过一个土地的组，并检查信息来源是否不同但其他属性相同
            List<List<LandDuplicationParam>> duplicates = group.entrySet().stream()
                    .filter(entry -> entry.getValue().size() > 1)
                    .map(entry -> {
                        List<LandDuplicationParam> dupList = entry.getValue();
                        return dupList.stream()
                                .filter(s -> dupList.stream()
                                        .anyMatch(other -> {
                                            // 检查两个Source是否都不为null且不相等，或者其中一个为null而另一个不为null
                                            return (s.getSource() != null && other.getSource() != null && !s.getSource().equals(other.getSource()))
                                                    || (s.getSource() == null && other.getSource() != null)
                                                    || (s.getSource() != null && other.getSource() == null);
                                        }))
                                .collect(Collectors.toList());
                    })
                    .filter(list -> !list.isEmpty())//&& list.stream().allMatch(user -> user.getHeight() != 0 && user.getWeight() != 0)) // 过滤出非空的重复列表
                    .collect(Collectors.toList());

            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {

                    LandDuplicationParam param = new LandDuplicationParam();
                    param.setCount(duplicate.size());
                    param.setCityCaption(citys);

                    String ids = "";

                    List<Long> idList = new ArrayList<>();

                    for (int i = 0; i < duplicate.size(); i++) {
                        if (duplicate.get(i).getLandArea() == 0 || duplicate.get(i).getPrice() == 0 || StringUtils.isEmpty(duplicate.get(i).getRegion())) {
                            continue;
                        }
                        idList.add(duplicate.get(i).getId());
                        ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                    }
                    if (!CollectionUtils.isEmpty(idList)) {
                        //根据idList  获取landCaption
                        String landCaption = this.getLandCaptionById(idList, sqlCookie);
                        param.setLandCaption(landCaption);
                        param.setIdList(ids);
                        param.setCheckDate(publicMethod.getNowDate());
                        param.setRepeatGoal(replaceGoalNum++);
                        param.setDuplicationReason("2");
                        needList.add(param);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(needList)) {
                insertCombineLand(needList, sqlCookie, "2", citys);
            }

          /*  List<LandBaseParam> landList = new ArrayList<>();


            JSONObject param1 = new JSONObject();
            JSONObject param3 = new JSONObject();
            JSONObject param4 = new JSONObject();
            JSONObject param5 = new JSONObject();
            JSONObject param6 = new JSONObject();
            param6.put("State", 1);
            param6.put("ColumnId", "77");
            param6.put("Value>=", "2023-11-01");
            param6.put("@column", "Buildingid");
            param5.put("Land_Shu", param6);
            param5.put("from", "Land_Shu");
            param4.put("@column", "count(1):num");
            param3.put("state", 1);
            param3.put("CityCaption", citys);
            param3.put("id@", "/Land_Shu/Buildingid");
            param4.put("State", 1);
            param4.put("Buildingid{}@", "sql");

            param4.put("Columnid{}", columnListTwo);
            param1.put("join", "&/Internal_Land_BaseInfo_Main/id@");
            param1.put("Land_Shu", param4); //左表
            param1.put("Internal_Land_BaseInfo_Main", param3); //右表

            net.sf.json.JSONObject param2 = new net.sf.json.JSONObject();

            param2.put("sql@", param5);
            param2.put("[]", param1);

            String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
            net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
            int newCount = 0;
            if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
                for (Object s1 : sqlData.getJSONArray("[]")) {
                    net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Shu");
                    newCount = details.getInt("num");
                }
            }

            param3.put("@column", "id");
            param3.put("@order", "id-");
            param4.put("@column", "Columnid,Value,Buildingid");

            param1.put("Land_Shu", param4); //左表
            param1.put("Internal_Land_BaseInfo_Main", param3); //右表
            int cycleNum = 0;
            if (newCount > 50000) {
                cycleNum = newCount / 50000 + 1;
            }
            for (int page = 0; page <= cycleNum; page++) {

                param1.put("page", page);
                param1.put("count", 50000);
                param2.put("[]", param1);

                long start = System.currentTimeMillis();
                String ssr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
                long end = System.currentTimeMillis();
                long differ = end - start;
                time = time + differ;

                ObjectMapper mapper = new ObjectMapper();
                JsonNode rootNode = mapper.readTree(ssr);
                JsonNode jsonNode = rootNode.get("[]");
                if (jsonNode != null) {

                    for (JsonNode node : jsonNode) {

                        JsonNode detailsMain = node.get("Internal_Land_BaseInfo_Main");
                        JsonNode detailsShu = node.get("Land_Shu");

                        LandBaseParam duplication = new LandBaseParam();

                        if (detailsMain.has("id")) {


                            JsonNode longValueNode = detailsMain.get("id");
                            if (longValueNode != null) {
                                // 安全地获取long值
                                long id = longValueNode.asLong();
                                duplication.setId(id);
                            }
                            JsonNode cityValue = detailsMain.get("CityCaption");
                            if (cityValue != null) {
                                // 安全地获取String的值
                                String city = cityValue.asText();
                                duplication.setCityCaption(city);
                            }

                            JsonNode landCaption = detailsMain.get("LandCaption");
                            if (landCaption != null) {
                                // 安全地获取String的值
                                String land = landCaption.asText();
                                duplication.setLandCaption(land);
                            }

                            JsonNode Columnid = detailsShu.get("Columnid");
                            if (Columnid != null) {
                                // 安全地获取String的值
                                String columnid = Columnid.asText();
                                duplication.setColumnId(columnid);
                            }

                            JsonNode Value = detailsShu.get("Value");
                            if (Value != null) {
                                // 安全地获取String的值
                                String value = Value.asText();
                                duplication.setValue(value);
                            }
                            landList.add(duplication);
                        }
                    }
                }
            }

            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = landList.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));

            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());

                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }
                    //底价
                    if (StringUtils.equals("122", columnValue.getColumnId())) {
                        //todo  需要校验中文情况 ,抛出异常 ,然后禁止处理这条数据
                        if (!StringUtils.isEmpty(columnValue.getValue())) {
                            if (StringUtils.contains(columnValue.getValue(), ".")) {
                                String strPrice = columnValue.getValue();
                                int index = columnValue.getValue().indexOf(".");
                                if (index != -1) {
                                    strPrice = strPrice.substring(0, index);
                                }
                                // 将处理后的字符串转换为BigInteger
                                //  BigInteger number = new BigInteger(strPrice);
                                allLandParam.setPrice(Long.valueOf(strPrice));

                            } else {
                                allLandParam.setPrice(Long.valueOf(columnValue.getValue()));
                            }
                        }
                    }
                    //占地面积
                    if (StringUtils.equals("12", columnValue.getColumnId())) {
                        if (!StringUtils.isEmpty(columnValue.getValue())) {
                            if (StringUtils.contains(columnValue.getValue(), ".")) {
                                double number = Double.parseDouble(columnValue.getValue());
                                int truncatedNumber = (int) number;
                                allLandParam.setLandArea(truncatedNumber);
                            } else {
                                allLandParam.setLandArea(Integer.valueOf(columnValue.getValue()));
                            }
                        }
                    }

                }
                allList.add(allLandParam);
            }


          *//*  Map<List<Object>, LandDuplicationParam> uniqueUsers = allList.stream()
                    .collect(Collectors.toMap(
                            s -> Arrays.asList(s.getLandArea(), s.getPrice(), s.getRegion()), // 键：LandArea、Price、Region的列表
                            Function.identity(), // 值：对象本身
                            (existing, replacement) -> existing // 如果有冲突，保留现有的对象
                    ));

            // 将Map的值转换回List
            List<LandDuplicationParam> filteredUsers = new ArrayList<>(uniqueUsers.values());*//*

            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> group = allList.stream()
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getLandArea(), s.getPrice(), s.getRegion())
                    ));


            // 过滤出含有超过一个土地的组，并检查信息来源是否不同但其他属性相同
            List<List<LandDuplicationParam>> duplicates = group.entrySet().stream()
                    .filter(entry -> entry.getValue().size() > 1)
                    .map(entry -> {
                        List<LandDuplicationParam> dupList = entry.getValue();
                        return dupList.stream()
                                .filter(s -> dupList.stream()
                                        .anyMatch(other -> {
                                            // 检查两个Source是否都不为null且不相等，或者其中一个为null而另一个不为null
                                            return (s.getSource() != null && other.getSource() != null && !s.getSource().equals(other.getSource()))
                                                    || (s.getSource() == null && other.getSource() != null)
                                                    || (s.getSource() != null && other.getSource() == null);
                                        }))
                                .collect(Collectors.toList());
                    })
                    .filter(list -> !list.isEmpty())//&& list.stream().allMatch(user -> user.getHeight() != 0 && user.getWeight() != 0)) // 过滤出非空的重复列表
                    .collect(Collectors.toList());


            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {

                    LandDuplicationParam param = new LandDuplicationParam();
                    param.setCount(duplicate.size());
                    param.setCityCaption(citys);

                    String ids = "";

                    List<Long> idList = new ArrayList<>();

                    for (int i = 0; i < duplicate.size(); i++) {
                        //   BigInteger price = duplicate.get(i).getPrice();
                        if (duplicate.get(i).getLandArea() == 0 || duplicate.get(i).getPrice() == 0 || StringUtils.isEmpty(duplicate.get(i).getRegion())) {
                            continue;
                        }
                        idList.add(duplicate.get(i).getId());
                        ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                    }
                    if (!CollectionUtils.isEmpty(idList)) {
                        //根据idList  获取landCaption
                        String landCaption = this.getLandCaptionById(idList, sqlCookie);
                        param.setLandCaption(landCaption);
                        param.setIdList(ids);
                        param.setCheckDate(publicMethod.getNowDate());
                        param.setRepeatGoal(replaceGoalNum++);
                        param.setDuplicationReason("2");
                        needList.add(param);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(needList)){

                insertCombineLand(needList, sqlCookie, "2", citys);
            }
*/

        }
        log.error("Two:{}", System.currentTimeMillis());
        log.error("timeTwo:{}", time);
    }

    //将土地的竖表和主表 ,取出想要的字段 , 组成一张新的横表.
    @XxlJob("combineLandThree")
    public void combineLandThree() throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = null;
        try {
            //参数
            String str = "{\"phone\":\"18915047090\",\"password\":\"111111\"}";
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/login";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
        }
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        List<String> cookieList = httpHeaders.get("Set-Cookie");
        String sqlCookie = cookieList.get(0);
        //获取城市列表
         List<String> cityList = publicMethod.getAllCity(sqlCookie);
        //ArrayList<String> cityList = new ArrayList<>();
        cityList.add("测试");

        List<Integer> columnListThree = new ArrayList<>();
        columnListThree.add(3);
        columnListThree.add(276);
        columnListThree.add(98);

        log.error("Three开始:{}", System.currentTimeMillis());
        String repeatNum = publicMethod.getNowDateShort();
        Long replaceGoalNum = Long.valueOf("3" + repeatNum.replace("-", "") + "00000");
        long time = 0;
        for (String citys : cityList) {

            List<LandBaseParam> landBaseList = landRepeatMapper.getLandByAllCountry(citys, columnListThree);

            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = landBaseList.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));

            List<LandDuplicationParam> needList = new ArrayList<>();

            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());
                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }

                    //宗地编号
                    if (StringUtils.equals("98", columnValue.getColumnId())) {
                        String values=columnValue.getValue();
                        String modifiedString = values.replaceAll("[^\\p{IsAlphabetic}\\p{IsDigit}]", "");
                        allLandParam.setLandNum(modifiedString);
                      //  allLandParam.setLandNum(columnValue.getValue());
                    }
                }
                allList.add(allLandParam);
            }

            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> group = allList.stream().filter(
                    s -> !StringUtils.isEmpty(s.getLandNum()))
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getLandNum(), s.getRegion())
                    ));
            // 过滤出含有超过一个土地的组，并检查信息来源是否不同但其他属性相同
            List<List<LandDuplicationParam>> duplicates = group.entrySet().stream()
                    .filter(entry -> entry.getValue().size() > 1)
                    .map(entry -> {
                        List<LandDuplicationParam> dupList = entry.getValue();
                        return dupList.stream()
                                .filter(s -> dupList.stream()
                                        .anyMatch(other -> {
                                            // 检查两个Source是否都不为null且不相等，或者其中一个为null而另一个不为null
                                            return (s.getSource() != null && other.getSource() != null && !s.getSource().equals(other.getSource()))
                                                    || (s.getSource() == null && other.getSource() != null)
                                                    || (s.getSource() != null && other.getSource() == null);
                                        }))
                                .collect(Collectors.toList());
                    })
                    .filter(list -> !list.isEmpty()) // 过滤出非空的重复列表
                    .collect(Collectors.toList());
            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {

                    LandDuplicationParam param = new LandDuplicationParam();
                    param.setCount(duplicate.size());
                    param.setCityCaption(citys);
                    param.setLandNum(duplicate.get(0).getLandNum());
                    List<Long> idList = new ArrayList<>();
                    String ids = "";
                    for (int i = 0; i < duplicate.size(); i++) {
                        if (StringUtils.isEmpty(duplicate.get(i).getRegion()) || StringUtils.isEmpty(duplicate.get(i).getLandNum())) {
                            continue;
                        }
                        idList.add(duplicate.get(i).getId());
                        ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                    }
                    if (!CollectionUtils.isEmpty(idList)) {
                        //根据idList  获取landCaption
                        String landCaption = this.getLandCaptionById(idList, sqlCookie);
                        param.setLandCaption(landCaption);
                        param.setIdList(ids);
                        param.setCheckDate(publicMethod.getNowDate());
                        param.setRepeatGoal(replaceGoalNum++);
                        param.setDuplicationReason("3");
                        needList.add(param);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(needList)) {
                insertCombineLand(needList, sqlCookie, "3", citys);
            }
        }
        log.error("Three:{}", System.currentTimeMillis());
        log.error("timeThree:{}", time);
        // columnListThree.add(77);
/*
            List<LandBaseParam> landList = new ArrayList<>();
            for (Integer integer : columnListThree) {

                JSONObject param1 = new JSONObject();
                JSONObject param3 = new JSONObject();
                JSONObject param4 = new JSONObject();
                JSONObject param5 = new JSONObject();
                JSONObject param6 = new JSONObject();
                param6.put("State", 1);
                param6.put("ColumnId", "77");
                param6.put("Value>=", "2023-11-01");
                param6.put("@column", "Buildingid");
                param5.put("Land_Shu", param6);
                param5.put("from", "Land_Shu");
                param4.put("@column", "count(1):num");
                param3.put("state", 1);
                param3.put("CityCaption", citys);
                param3.put("id@", "/Land_Shu/Buildingid");
                param4.put("State", 1);
                param4.put("Buildingid{}@", "sql");

                param4.put("Columnid", integer);
                param1.put("join", "&/Internal_Land_BaseInfo_Main/id@");
                param1.put("Land_Shu", param4); //左表
                param1.put("Internal_Land_BaseInfo_Main", param3); //右表

                net.sf.json.JSONObject param2 = new net.sf.json.JSONObject();

                param2.put("sql@", param5);
                param2.put("[]", param1);

                String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
                net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
                int newCount = 0;
                if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
                    for (Object s1 : sqlData.getJSONArray("[]")) {
                        net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Shu");
                        newCount = details.getInt("num");
                    }
                }

                param3.put("@column", "id");
                param3.put("@order", "id-");
                param4.put("@column", "Columnid,Value,Buildingid");
                param1.put("Land_Shu", param4); //左表
                param1.put("Internal_Land_BaseInfo_Main", param3); //右表
                int cycleNum = 0;
                if (newCount > 50000) {
                    cycleNum = newCount / 50000 + 1;
                }
                for (int page = 0; page <= cycleNum; page++) {

                    param1.put("page", page);
                    param1.put("count", 50000);
                    param2.put("[]", param1);
                    long start = System.currentTimeMillis();
                    String ssr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
                    long end = System.currentTimeMillis();
                    long differ = end - start;
                    time = time + differ;
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode rootNode = mapper.readTree(ssr);
                    JsonNode jsonNode = rootNode.get("[]");
                    if (jsonNode != null) {

                        for (JsonNode node : jsonNode) {

                            JsonNode detailsMain = node.get("Internal_Land_BaseInfo_Main");
                            JsonNode detailsShu = node.get("Land_Shu");

                            LandBaseParam duplication = new LandBaseParam();
                            if (detailsMain.has("id")) {
                                JsonNode longValueNode = detailsMain.get("id");
                                if (longValueNode != null) {
                                    // 安全地获取long值
                                    long id = longValueNode.asLong();
                                    duplication.setId(id);
                                }
                                JsonNode cityValue = detailsMain.get("CityCaption");
                                if (cityValue != null) {
                                    // 安全地获取String的值
                                    String city = cityValue.asText();
                                    duplication.setCityCaption(city);
                                }

                                JsonNode Columnid = detailsShu.get("Columnid");
                                if (Columnid != null) {
                                    // 安全地获取String的值
                                    String columnid = Columnid.asText();
                                    duplication.setColumnId(columnid);
                                }

                                JsonNode Value = detailsShu.get("Value");
                                if (Value != null) {
                                    // 安全地获取String的值
                                    String value = Value.asText();
                                    duplication.setValue(value);
                                }
                                landList.add(duplication);
                            }
                        }
                    }
                }
            }
            //将id相同 ,column不同的数据 .分组到一起
            Map<Long, List<LandBaseParam>> GroupsById = landList.stream()
                    .collect(Collectors.groupingBy(LandBaseParam::getId));

            List<LandDuplicationParam> allList = new ArrayList<>();
            for (List<LandBaseParam> value : GroupsById.values()) {
                LandDuplicationParam allLandParam = new LandDuplicationParam();
                LandBaseParam param = value.get(0);
                allLandParam.setId(param.getId());
                //    allLandParam.setCityCaption(param.getCityCaption());
                //    allLandParam.setLandCaption(param.getLandCaption());
                for (int i = 0; i < value.size(); i++) {
                    LandBaseParam columnValue = value.get(i);
                    //区域
                    if (StringUtils.equals("3", columnValue.getColumnId())) {
                        allLandParam.setRegion(columnValue.getValue());
                    }
                    //信息来源
                    if (StringUtils.equals("276", columnValue.getColumnId())) {
                        allLandParam.setSource(columnValue.getValue());
                    }

                    //宗地编号
                    if (StringUtils.equals("98", columnValue.getColumnId())) {
                        allLandParam.setLandNum(columnValue.getValue());
                    }
                }
                allList.add(allLandParam);
            }

            // 根据条件找出重复的土地
            Map<List<Object>, List<LandDuplicationParam>> group = allList.stream().filter(
                    s -> !StringUtils.isEmpty(s.getLandNum()))
                    .collect(Collectors.groupingBy(
                            s -> Arrays.asList(s.getLandNum(), s.getRegion())
                    ));
            // 过滤出含有超过一个土地的组，并检查信息来源是否不同但其他属性相同
            List<List<LandDuplicationParam>> duplicates = group.entrySet().stream()
                    .filter(entry -> entry.getValue().size() > 1)
                    .map(entry -> {
                        List<LandDuplicationParam> dupList = entry.getValue();
                        return dupList.stream()
                                .filter(s -> dupList.stream()
                                        .anyMatch(other -> {
                                            // 检查两个Source是否都不为null且不相等，或者其中一个为null而另一个不为null
                                            return (s.getSource() != null && other.getSource() != null && !s.getSource().equals(other.getSource()))
                                                    || (s.getSource() == null && other.getSource() != null)
                                                    || (s.getSource() != null && other.getSource() == null);
                                        }))
                                .collect(Collectors.toList());
                    })
                    .filter(list -> !list.isEmpty()) // 过滤出非空的重复列表
                    .collect(Collectors.toList());
            for (List<LandDuplicationParam> duplicate : duplicates) {
                if (duplicate.size() > 1) {

                    LandDuplicationParam param = new LandDuplicationParam();
                    param.setCount(duplicate.size());
                    param.setCityCaption(citys);
                    param.setLandNum(duplicate.get(0).getLandNum());
                    List<Long> idList = new ArrayList<>();
                    String ids = "";
                    for (int i = 0; i < duplicate.size(); i++) {
                        if (StringUtils.isEmpty(duplicate.get(i).getRegion()) || StringUtils.isEmpty(duplicate.get(i).getLandNum())) {
                            continue;
                        }
                        idList.add(duplicate.get(i).getId());
                        ids = ids + String.valueOf(duplicate.get(i).getId()) + ";";
                    }
                    if (!CollectionUtils.isEmpty(idList)) {
                        //根据idList  获取landCaption
                        String landCaption = this.getLandCaptionById(idList, sqlCookie);
                        param.setLandCaption(landCaption);
                        param.setIdList(ids);
                        param.setCheckDate(publicMethod.getNowDate());
                        param.setRepeatGoal(replaceGoalNum++);
                        param.setDuplicationReason("3");
                        needList.add(param);
                    }
                }
            }
            insertCombineLand(needList, sqlCookie, "3", citys);
        }*/

    }


    public void insertCombineLand(List<LandDuplicationParam> needList, String cookie, String condition /*情况*/, String citys) {
        //如果第一天被判重，然后他只修改了数据没在查重工具改状态，第二天没被判重，然后自动把它的状态改成已处理。
        //返回了重复数据
        List<String> commonIds = updateLandState(needList, cookie, condition, citys);

        //needList 中的数据 ,也需要去除掉commonIds中的数据 ,再插入.
        if (!CollectionUtils.isEmpty(commonIds)) {

            List<LandDuplicationParam> filter = needList.stream()
                    .filter(s -> !commonIds.contains(s.getIdList()))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(filter)) {

                com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                JSONArray array = new JSONArray();
                for (LandDuplicationParam param : filter) {
                    com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
                    object.put("idList", param.getIdList());
                    object.put("landCaption", param.getLandCaption());
                    object.put("cityCaption", param.getCityCaption());
                    object.put("count", param.getCount());
                    object.put("duplicationReason", param.getDuplicationReason());
                    object.put("checkDate", param.getCheckDate());
                    object.put("repeatGoal", param.getRepeatGoal());
                    if (!StringUtils.isEmpty(param.getLandNum())) {
                        object.put("landNum", param.getLandNum());
                    }
                    object.put("State", "1");
                    object.put("processState", "未处理");
                    array.add(object);
                }
                jsonObject.put("Land_Main_Shu_Repeat[]", array);
                jsonObject.put("tag", "Land_Main_Shu_Repeat[]");
                ResponseEntity<String> stringResponseEntity = restTemplateUtil.restTemplatePostByInsertInfo(jsonObject.toString(), cookie);
                String body = stringResponseEntity.getBody();
                JSONObject data = JSONObject.fromObject(body);
                if (data.getBoolean("ok") != true) {
                    log.error("error Message :[{}]", data.getString("msg"));
                }
            }
        } else {
            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            JSONArray array = new JSONArray();
            for (LandDuplicationParam param : needList) {
                com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
                object.put("idList", param.getIdList());
                object.put("landCaption", param.getLandCaption());
                object.put("cityCaption", param.getCityCaption());
                object.put("count", param.getCount());
                object.put("duplicationReason", param.getDuplicationReason());
                object.put("checkDate", param.getCheckDate());
                object.put("repeatGoal", param.getRepeatGoal());
                object.put("State", "1");
                if (!StringUtils.isEmpty(param.getLandNum())) {
                    object.put("landNum", param.getLandNum());
                }
                object.put("processState", "未处理");
                array.add(object);
            }
            jsonObject.put("Land_Main_Shu_Repeat[]", array);
            jsonObject.put("tag", "Land_Main_Shu_Repeat[]");
            ResponseEntity<String> stringResponseEntity = restTemplateUtil.restTemplatePostByInsertInfo(jsonObject.toString(), cookie);
            String body = stringResponseEntity.getBody();
            JSONObject data = JSONObject.fromObject(body);
            if (data.getBoolean("ok") != true) {
                log.error("error Message :[{}]", data.getString("msg"));
            }

        }
        // 去重目标的更新
        // 查询全表的已处理数据  ,再查询全表的未处理数据 , 找出交集 ,把已处理数据的去重目标赋值给未处理数据的去重目标 .
        updateGoal(cookie, citys);
    }

    private void updateGoal(String cookie, String citys) {
        ArrayList<String> listReason = new ArrayList<>(Arrays.asList("1", "2", "3"));
        for (String s : listReason) {

            com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
            param.put("@column", "count(1):num");
            param.put("State", "1");
            param.put("duplicationReason", s);
            param.put("cityCaption", citys);
            List<String> list1 = new ArrayList<>();
            list1.add("未处理");
            list1.add("已处理");
            param.put("processState{}", list1);
            com.alibaba.fastjson.JSONObject param1 = new com.alibaba.fastjson.JSONObject();
            param1.put("Land_Main_Shu_Repeat", param);
            param1.put("page", 0);
            param1.put("count", 50000);
            com.alibaba.fastjson.JSONObject param2 = new com.alibaba.fastjson.JSONObject();
            param2.put("[]", param1);


            String srNum = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
            net.sf.json.JSONObject sqlDataNum = net.sf.json.JSONObject.fromObject(srNum);
            int newCount = 0;
            if (sqlDataNum.getBoolean("ok") == true && sqlDataNum.containsKey("[]")) {
                for (Object s1 : sqlDataNum.getJSONArray("[]")) {
                    net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                    newCount = details.getInt("num");
                }
            }

            int cycleNum = 0;
            if (newCount > 50000) {
                cycleNum = newCount / 50000 + 1;
            }
            List<LandDuplicationParam> list = new ArrayList<>();
            for (int page = 0; page <= cycleNum; page++) {
                param.put("@column", "id,idList,repeatGoal,processState");
                param1.put("page", page);
                param1.put("count", 50000);
                param1.put("Land_Main_Shu_Repeat", param);
                param2.put("[]", param1);
                String sr = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
                net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
                if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
                    for (Object s1 : sqlData.getJSONArray("[]")) {
                        net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                        LandDuplicationParam landDuplicationParam = new LandDuplicationParam();
                        landDuplicationParam.setIdList(details.getString("idList"));
                        landDuplicationParam.setId(details.getLong("id"));
                        landDuplicationParam.setRepeatGoal(details.getLong("repeatGoal"));
                        landDuplicationParam.setProcessState(details.getString("processState"));
                        list.add(landDuplicationParam);
                    }
                }
            }

            Map<String, List<LandDuplicationParam>> groupedById = list.stream()
                    .collect(Collectors.groupingBy(LandDuplicationParam::getIdList));

            //   Map<String, List<LandDuplicationParam>> groupedById = Stream.concat(handleList.stream(), noHandleList.stream())
            //         .collect(Collectors.groupingBy(LandDuplicationParam::getIdList));

            // 将分组结果转换为List<List<LandDuplicationParam>>形式
            for (List<LandDuplicationParam> WithSameId : groupedById.values()) {
                if (WithSameId.size() > 1) { // 只保留那些来自两个列表的数据
                    if (StringUtils.equals(WithSameId.get(0).getProcessState(), "未处理")) {
                        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
                        object.put("id", WithSameId.get(0).getId());
                        object.put("repeatGoal", WithSameId.get(1).getRepeatGoal());
                        jsonObject.put("Land_Main_Shu_Repeat", object);
                        jsonObject.put("tag", "Land_Main_Shu_Repeat");

                        restTemplateUtil.restTemplatePostByUpdateInfo(jsonObject.toString(), cookie);
                    } else {
                        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
                        object.put("id", list.get(1).getId());
                        object.put("repeatGoal", list.get(0).getRepeatGoal());
                        jsonObject.put("Land_Main_Shu_Repeat", object);
                        jsonObject.put("tag", "Land_Main_Shu_Repeat");

                        restTemplateUtil.restTemplatePostByUpdateInfo(jsonObject.toString(), cookie);
                    }
                }
            }
        }
    }

    private List<String> updateLandState(List<LandDuplicationParam> needList, String cookie, String condition, String citys) {
        // 要求 :如果第一天被判重，然后用户只修改了数据没在查重工具改状态，第二天没被判重，然后自动把它的状态改成已处理   操作时间和查重时间不变

        //这一步需要先查询出已有的重复土地中的 和当天查询出来的数据 .
        // 比较 ,当天查询的数据比已有的少了哪些. 那就说明已有的那些重复数据被修正了,
        //总结  库里已有的比当天查询出来的多的那部分  ,改成已处理.
        Map<String, Map<String, String>> untreatedLand = this.getUntreatedLand(cookie, condition, citys);


        List<String> ignoreId = new ArrayList<>();
        if (untreatedLand.containsKey("已忽略")) {
            Map<String, String> map = untreatedLand.get("已忽略");
            ignoreId = map.keySet().stream()
                    .collect(Collectors.toList());
        }
        //当天需要插入的
        List<String> allId = needList.stream()
                .map(LandDuplicationParam::getIdList)
                .collect(Collectors.toList());

        if (untreatedLand.containsKey("未处理")) {
            Map<String, String> map = untreatedLand.get("未处理");

            List<String> mapKey = map.keySet().stream().collect(Collectors.toList());
            List<String> list = new ArrayList<>(mapKey);
            //原有的未处理数据中   去除当天判定重复的数据 , 剩下的都是 ,用户已经修改数据,但是管理界面没修改状态
            mapKey.removeAll(allId);
            //再取出id去修改   此时要将未处理,改成已处理
            if (!CollectionUtils.isEmpty(mapKey)) {
                List<String> updateId = mapKey.stream()
                        .filter(map::containsKey)
                        .map(map::get)
                        .collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(updateId)) {
                    //需要把allId 里的数据 ,全部改成已处理.
                    com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                    com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
                    object.put("id{}", updateId);
                    object.put("processState", "已处理");
                    jsonObject.put("Land_Main_Shu_Repeat", object);
                    jsonObject.put("tag", "Land_Main_Shu_Repeat");

                    ResponseEntity<String> stringResponseEntity = restTemplateUtil.restTemplatePostByUpdateInfo(jsonObject.toString(), cookie);
                    String body = stringResponseEntity.getBody();
                    JSONObject data = JSONObject.fromObject(body);
                    if (data.getBoolean("ok") != true) {
                        log.error("error Message :[{}]", data.getString("msg"));
                    }
                }
            }

            list.addAll(ignoreId);
            return list;
        }
        return ignoreId;
    }

    public Map<String, Map<String, String>> getUntreatedLand(String cookie, String condition, String citys) {
        com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
        param.put("@column", "count(1):num");
        param.put("State", "1");
        param.put("cityCaption", citys);
        param.put("processState", "未处理");
        param.put("duplicationReason", condition);  //重复原因 -情况一
        com.alibaba.fastjson.JSONObject param1 = new com.alibaba.fastjson.JSONObject();
        param1.put("Land_Main_Shu_Repeat", param);

        com.alibaba.fastjson.JSONObject param2 = new com.alibaba.fastjson.JSONObject();
        param2.put("[]", param1);

        String srNum = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
        net.sf.json.JSONObject sqlDataNum = net.sf.json.JSONObject.fromObject(srNum);
        int newCount = 0;
        if (sqlDataNum.getBoolean("ok") == true && sqlDataNum.containsKey("[]")) {
            for (Object s1 : sqlDataNum.getJSONArray("[]")) {
                net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                newCount = details.getInt("num");
            }
        }
        int cycleNum = 0;
        if (newCount > 50000) {
            cycleNum = newCount / 50000 + 1;
        }
        Map<String, String> untreatedMap = new HashMap<>();
        Map<String, Map<String, String>> map = new HashMap<>();
        for (int page = 0; page <= cycleNum; page++) {

            param.put("@column", "id,idList");
            param1.put("page", page);
            param1.put("count", 50000);
            param1.put("Land_Main_Shu_Repeat", param);
            param2.put("[]", param1);
            String sr = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
            net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
            if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
                for (Object s1 : sqlData.getJSONArray("[]")) {
                    net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                    untreatedMap.put(details.getString("idList"), details.getString("id"));
                }
            }
        }
        if (!untreatedMap.isEmpty()) {
            map.put("未处理", untreatedMap);
        }
        param.put("@column", "count(1):num");
        param.put("processState", "已忽略");
        param1.put("Land_Main_Shu_Repeat", param);
        param2.put("[]", param1);
        String ssrNum = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
        net.sf.json.JSONObject sqlDataNum1 = net.sf.json.JSONObject.fromObject(ssrNum);
        int newCount1 = 0;
        if (sqlDataNum1.getBoolean("ok") == true && sqlDataNum1.containsKey("[]")) {
            for (Object s1 : sqlDataNum1.getJSONArray("[]")) {
                net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                newCount1 = details.getInt("num");
            }
        }
        int cycleNum1 = 0;
        if (newCount1 > 50000) {
            cycleNum1 = newCount1 / 50000 + 1;
        }
        Map<String, String> ignoreMap = new HashMap<>();
        for (int page = 0; page <= cycleNum1; page++) {
            param1.put("page", page);
            param1.put("count", 50000);
            param.put("@column", "id,idList");
            param1.put("Land_Main_Shu_Repeat", param);
            param2.put("[]", param1);
            String ssr = HttpRequest.sendPostAPI("get", param2.toString(), cookie);
            net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(ssr);
            if (data.getBoolean("ok") == true && data.containsKey("[]")) {
                for (Object s1 : data.getJSONArray("[]")) {
                    net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");
                    ignoreMap.put(details.getString("idList"), details.getString("id"));
                }
            }

        }
        if (!ignoreMap.isEmpty()) {
            map.put("已忽略", ignoreMap);
        }
        return map;
    }


    public String getLandCaptionById(List<Long> idList, String sqlCookie) {
        com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
        param.put("@column", "LandCaption");
        param.put("State", "1");
        param.put("id{}", idList);
        com.alibaba.fastjson.JSONObject param1 = new com.alibaba.fastjson.JSONObject();
        param1.put("Internal_Land_BaseInfo_Main", param);
        param1.put("page", 0);
        param1.put("count", 1000);
        com.alibaba.fastjson.JSONObject param2 = new com.alibaba.fastjson.JSONObject();
        param2.put("[]", param1);

        String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
        String LandCaption = "";
        net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
        if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
            for (Object s1 : sqlData.getJSONArray("[]")) {
                net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Internal_Land_BaseInfo_Main");
                if (details.containsKey("LandCaption")) {
                    LandCaption = LandCaption + details.getString("LandCaption") + ";";
                }

            }
        }
        return LandCaption;
    }
}
